package edu.xja.controller;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;


public class BaseController extends HttpServlet {
    //分页条数
    public static final int PAGE_SIZE=2;
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        //获取请求url
        String requestUrl=request.getRequestURL().toString();
        //获取调用方法的名称，最后一个斜线后面的字符串
        String action=requestUrl.substring(requestUrl.lastIndexOf("/")+1);
        //获取字节码对象
        Class cls=this.getClass();
        try {
            //获取方法
            Method method=cls.getMethod(action,HttpServletRequest.class,HttpServletResponse.class);
            //调用方法，返回转发的视图
            String returnView= (String) method.invoke(this,request,response);
            if(null!=returnView) {
                request.getRequestDispatcher(returnView).forward(request, response);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
